package malus.shaofeng.com.beiwomusic.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;


/**
 * 存储一写sharedPreferences信息
 */
public class SPHelper {

        private static Context context;
        private static SPHelper instance;
        private SPHelper(Context context){
            this.context=context;
        }
        public static SPHelper getInstance(){
            if(context==null){
                throw new IllegalArgumentException("SPHelper must init before use");
            }
            return instance;
        }
        public static void initSPHelper(Context context){
            if(context==null){
                throw new IllegalArgumentException("context must not null");
            }
            instance=new SPHelper(context);
        }

        /**
         * 保存token信息
         */
        public void setToken(String token){
            SharedPreferences sp = context.getSharedPreferences(
                    Constants.SP.KEY_SP_TABLE_LOGIN_USER
                    , Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putString(Constants.SP.KEY_SP_TOKEN, token);
            edit.commit();
        }

        /**
         * 获取token信息,如果没有则返回null
         */
        public String getToken(){
            SharedPreferences sp = context.getSharedPreferences(
                    Constants.SP.KEY_SP_TABLE_LOGIN_USER
                    , Context.MODE_PRIVATE);

            String string = sp.getString(Constants.SP.KEY_SP_TOKEN, "");
            if(TextUtils.isEmpty(string)){
                return null;
            }
            return string;
        }
    }



